<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/10 0010
 * Time: 下午 5:10
 */

namespace Admin\Model;


use Think\Model;
use Think\Page;

class ArticleModel extends Model{

    /**
     * 添加文章
     *  @return int
     */
    public function addRow($data){
        //将日期转换为时间戳
        $data['inputtime'] = time($data['inputtime']);
        //获取文章内容,添加过滤
        $content = I('post.content','','htmlspecialchars');
        $article_id = $this->add($data);
        //构造data数据
        $articleContent = array(
            'article_id'=>$article_id,
            'content'=>$content,
        );
        //实例化
        $article_content = D('articleContent');
        //把当前id和文章内容绑定
        return $article_content->add($articleContent);
    }

    /**
     * 自动验证规则
     * @var array
     */

    protected $_validate = array(
        array('name', 'require', '必须有文章名'),
        array('name', '', '文章名已存在', self::EXISTS_VALIDATE, 'unique'),
        array('sort', 'number', '排序必须为数字', 2)
    );

    /**
     * 获取分页
     * @return array $rows
     * @param array $cond
     */
    public function getPageResult($cond = array(),$page=1){
        $cond['status'] = array('gt',-1);
        //获取总条数
        $count = $this->where($cond)->count();
        //设置显示条数
        $size = C('PAGE_SIZE');
        //实例化page传参
        $page_obj = new Page($count,$size);
        //分页设置样式
        $page_obj->setConfig('theme',C('PAGE_THEME'));
        //显示html
        $page_html = $page_obj->show();
        $rows = $this->where($cond)->page($page,$size)->order('sort')->select();
        return array(
            'page_html'=>$page_html,
            'rows'=> $rows,
        );
    }

    /**
     * 逻辑删除
     * @param $id
     * @return bool
     */
    public function getDel($id) {
        //构建要修改的对象
        $data = array(
            'status' => '-1',
            'name' => array('exp', "CONCAT(name,'_del')"),
        );
        return $this->where("id=$id")->setField($data);
    }

}